import java.util.Stack;

public class Sort {
    /*
    时间复杂度：最好：O(N)   最坏：O(N^2)
    空间复杂度：O(1)
    稳定性：稳定
     */

    //直接插入排序
    public static void insertSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int temp = arr[i];
            int j = i - 1;
            for (j = i - 1; j >= 0; j--) {
                if (arr[j] > temp) {
                    arr[j + 1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + 1] = temp;
        }
    }

    /*
    稳定性：不稳定
     */
    //希尔排序
    public static void shellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap /= 2;
            shell(arr, gap);
        }
    }

    //每组进行插入排序
    private static void shell(int[] arr, int gap) {
        //i++ 交替变量进行排序
        for (int i = gap; i < arr.length; i++) {
            int temp = arr[i];
            int j = i - gap;
            for (j = i - gap; j >= 0; j -= gap) {
                if (arr[j] > temp) {
                    arr[j + gap] = arr[j];
                } else {
                    break;
                }
            }
            arr[j + gap] = temp;
        }
    }

    //直接选择排序
    /*
    时间复杂度：O(N^2)
    空间复杂度：O(1)
    稳定性：不稳定
     */
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void selectSort1(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr, minIndex, i);
        }
    }

    public static void selectSort(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            int min = left;
            int max = left;
            for (int i = left + 1; i <= right; i++) {
                if (arr[i] < arr[min]) {
                    min = i;
                }
                if (arr[i] > arr[max]) {
                    max = i;
                }
            }
            swap(arr, min, left);
            if (max == left) {
                max = min;
            }
            swap(arr, max, right);
            //如果最大值是left下标，上面交换完成后，
            //最大值跑到最小值的位置，因此需要更新最大值下标
            left++;
            right--;
        }
    }

    //堆排序
    /*
    时间复杂度：O(N*logN)
    空间复杂度：O(N)
    稳定性：不稳定
     */
    private static void createBigHeap(int[] arr) {
        for (int parent = (arr.length - 1 - 1); parent >= 0; parent--) {
            siftDown(parent, arr, arr.length);
        }
    }

    private static void siftDown(int parent, int[] arr, int end) {
        int child = 2 * parent + 1;
        while (child < end) {
            if (child + 1 < end && arr[child] < arr[child + 1]) {
                child++;
            }
            //child下标 就是左右孩子最大值的下标
            if (arr[child] > arr[parent]) {
                swap(arr, child, parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public static void heapSort(int[] arr) {
        createBigHeap(arr);
        int end = arr.length - 1;
        while (end >= 0) {
            swap(arr, 0, end);
            siftDown(0, arr, end);
            end--;
        }
    }

    //冒泡排序
    /*
    时间复杂度：O(n^2)
    空间复杂度：O(N)
    稳定性：稳定
     */
    public static void bubbleSort(int[] arr) {
        boolean flg = false;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
            //如果第一次排序后是有序的，则结束循环
            //在优化情况下，时间复杂度：O(N)
            if (!flg) {
                break;
            }
        }
    }

    //快速排序
    /*
    时间复杂度：O(N*logN)
    空间复杂度：O(logN)
    稳定性：不稳定
    Hoare法
     */
    public static void quickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private static void quick(int[] arr, int start, int end) {
        if (start >= end) {
            return;
        }
        //三数取中法
        int index = midThreeNum(arr, start, end);
        swap(arr, index, end);
        int par = partition(arr, start, end);
        quick(arr, start, par - 1);
        quick(arr, par + 1, end);
    }

    private static int midThreeNum(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        if (arr[mid] < arr[right]) {
            if (arr[mid] < arr[left]) {
                return left;
            } else if (arr[mid] > arr[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            if (arr[mid] < arr[right]) {
                return right;
            } else if (arr[mid] > arr[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }

    private static int partition(int[] arr, int left, int right) {
        int i = left;
        int temp = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= temp) {
                right--;
            }
            while (left < right && arr[left] <= temp) {
                left++;
            }
            swap(arr, left, right);
        }
        //left 和 right 相遇
        swap(arr, left, i);
        return left;
    }

    private static int partition1(int[] arr, int left, int right) {
        int temp = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= temp) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= temp) {
                left++;
            }
            arr[right] = arr[left];
        }
        //left 和 right 相遇
        arr[left] = temp;
        return left;
    }

    //前后指针法
    private static int partition2(int[] arr, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if (arr[cur] < arr[left] && arr[++prev] != arr[cur]) {
                swap(arr, cur, prev);
            }
            cur++;
        }
        swap(arr, prev, left);
        return prev;
    }

    //快排非递归
    /*非递归-快排*/
    public void quickSortNor(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = arr.length - 1;
        int par = partition(arr, left, right);
        if (par - 1 > left) {
            stack.push(left);
            stack.push(par - 1);
        }
        if (par + 1 < right) {
            stack.push(par + 1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            par = partition(arr, left, right);
            if (par - 1 > left) {
                stack.push(left);
                stack.push(par - 1);
            }
            if (par + 1 < right) {
                stack.push(par + 1);
                stack.push(right);
            }
        }
    }

    //归并排序
    /*
    时间复杂度：O(N*logN)
    空间复杂度：O(logN)
    稳定性：稳定
     */
    public static void mergeSort(int[] arr) {
        mergeSortFun(arr, 0, arr.length - 1);
    }

    public static void mergeSortFun(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSortFun(arr, left, mid);
        mergeSortFun(arr, mid + 1, right);

        //合并数组
        merge(arr, left, mid, right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int[] temp = new int[right - left + 1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2) {
            if (arr[s1] <= arr[s2]) {
                temp[k++] = arr[s1++];
            } else {
                temp[k++] = arr[s2++];
            }
        }
        while (s1 <= e1) {
            temp[k++] = arr[s1++];
        }
        while (s2 <= e2) {
            temp[k++] = arr[s2++];
        }
        //temp数组已经有序了，把temp数组的内容拷贝到arr数组里
        for (int i = 0; i < k; i++) {
            arr[i + left] = temp[i];
        }
    }

    //归并排序的非递归实现
    public static void mergeSortNor(int[] arr) {
        int gap = 1;
        while (gap < arr.length) {
            for (int i = 0; i < arr.length; i = i + 2 * gap) {
                int left = i;
                int mid = left + gap - 1;
                if (mid >= arr.length) {
                    mid = arr.length - 1;
                }
                int right = mid + gap;
                if (right >= arr.length) {
                    right = arr.length - 1;
                }
                merge(arr, left, mid, right);
            }
            gap *= 2;
        }
    }

    //计数排序
     /*时间复杂度：O(MAX(N,范围))
     空间复杂度：O(范围)
     稳定性：稳定
     */
    public static void countSort(int[] arr) {
        //1.遍历数组 求最大值和最小值
        int minVal = arr[0];
        int maxVal = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > maxVal) {
                maxVal = arr[i];
            }
            if (arr[i] < minVal) {
                minVal = arr[i];
            }
        }
        //2.定义一个count数组
        int n = maxVal - minVal + 1;
        int[] count = new int[n];

        //3.遍历arr数组，把值放入计数数组中
        for (int i = 0; i < arr.length; i++) {
            int value = arr[i];
            count[value - minVal]++;
        }
        //4.遍历计数组
        int index = 0;
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                arr[index] = i + minVal;
                index++;
                count[i]--;
            }
        }
    }
}
